package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.dto.ConvertDto;
import com.csun.cmny.provider.dto.ConvertQueryDto;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcConvertService;
import com.csun.cmny.provider.validator.OmcGiftAddValidator;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-05-23 20:24
 * @Description: the class omc convert controller
 */
@RestController
@RequestMapping(value = "/convert", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcConvertController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcConvertService omcConvertService;

    /**
     * 礼品兑换
     *
     * @param convertDto    convert dto
     * @param br            binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    //@LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/add")
    public Wrapper addGift(@Validated(OmcGiftAddValidator.class) @RequestBody ConvertDto convertDto,
                           BindingResult br) {

        // 设置组织Id
        convertDto.setGroupId(getLoginAuthDto().getGroupId());

        Integer result = omcConvertService.addConvert(convertDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

//        return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 礼品兑换记录分页查询
     *
     * @param convertQueryDto   convert query dto
     *
     * @return  the wrapper.
     */
   // @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryConvertListWithPage(@RequestBody ConvertQueryDto convertQueryDto) {

        // 设置组织Id
        convertQueryDto.setGroupId(getLoginAuthDto().getGroupId());

        PageInfo pageInfo = omcConvertService.queryConvertListWithPage(convertQueryDto);

//        return WrapMapper.ok(pageInfo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource.getMessage("operation.success", null), pageInfo);
    }

}
